<?php

if (!defined("PHPUnit_MAIN_METHOD")) {
    define("PHPUnit_MAIN_METHOD", "RuleFeedRelationTest::main");
}

require_once "PHPUnit/Framework/TestCase.php";
require_once "PHPUnit/Framework/TestSuite.php";

require_once 'init_fu.php';

class RuleFeedRelationTest extends PHPUnit_Framework_TestCase {
	
	public function testCleanUp() {
		$this->assertTrue($this->rf->cleanUp(100));
	}
	
	public function testAdd() {
		$this->assertTrue($this->rf->addFeedForRule(12, 100));
	}

	public function testrm() {
		$this->assertTrue($this->rf->rmFeedForRule(12, 100));
	}

	public function testFeedOids() {
		$this->rf->addFeedForRule(12, 100);
		$this->rf->addFeedForRule(10, 100);
		$this->rf->addFeedForRule(11, 100);
		$this->assertEquals(array(12, 10, 11), $this->rf->getRuleOidsForFeed(100));
		$this->rf->cleanUp(100);
	}

	public function testRuleOids() {
		$this->rf->addFeedForRule(10, 100);
		$this->rf->addFeedForRule(10, 102);
		$this->assertEquals(array(102, 100), $this->rf->getFeedOidsForRule(10));
		$this->rf->cleanUp(100);
		$this->rf->cleanUp(102);
	}

    public static function main() {
        require_once "PHPUnit/TextUI/TestRunner.php";

        $suite  = new PHPUnit_Framework_TestSuite("RuleFeedRelationTest");
        $result = PHPUnit_TextUI_TestRunner::run($suite);
    }

    protected function setUp() {
    	global $FU;
    	require_once $FU->dir->lib.'RL/UseScript.php';

		$rh = new RL_UseScript();

		$rh->useClass("RL_Debug");
		$debug = new RL_Debug(/*min error level for halt*/3);
		
		$config_dir = $FU->dir->config;
		require "$config_dir/conf.d/db.php";
	
		$rh->useClass("RL_DBAL");
		$db = new RL_DBAL($debug);
		$db->loadConfig($config);
	
		// соединение с БД
		$db->connect();

		include_once 'includes/type.php';
	
		$td = TypeDomain::instance();
		$td->addLoader(new KarniTypeLoader("$config_dir/type.d"));
		$td->addLoader(new DocTypeLoader("$config_dir/conf.d/doctype.php"));

		// add default types
		$td->setType('atom'        , new TAtom());
		$td->setType('object'      , new TObject());
		$td->setType('collection'  , new TCollection(array()));
		$td->cloneType('string'    , 'atom');
		$td->cloneType('integer'   , 'atom');
		$td->cloneType('bool'      , 'atom');
		$td->aliasType('boolean'   , 'bool');
		$td->cloneType('text'      , 'atom');
		$td->cloneType('datetime'  , 'atom');
		$td->cloneType('timestamp' , 'atom');

		require "$config_dir/conf.d/store.php";
		require_once $config['code'].'/stores/'.$config['dbms'].'store.php';
		$store_type = $config['dbms'].'store';

		$store = new $store_type($config, $db, $td);
		
		$rh->UseClass("Model_ModelManager");
		$mm = new ModelManager( $rh, $db, $store );
		$this->rf = $mm->getModel("RuleFeedRelation");

    }

    protected function tearDown() {
    	unset($this->rf);
    }
}

if (PHPUnit_MAIN_METHOD == "RuleFeedRelationTest::main") {
    RuleFeedRelationTest::main();
}
?>
